# -*- coding: utf-8 -*-
import sys
sys.path.append('../')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import requests,execjs
from common.helper import Helper
from urllib.parse import urlencode
class Spider( object ):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "华夏逍遥行意外综合保险产品计划"
    # 生成产品数据
    def genProductInfo(self):
        plan = {

            "planA":{
                #年龄段
                "year":[str( i ) for i  in range(0,51)],
                #性别
                "gender":["M","F"],
                #保障期限
                "days":["25y","35y"],
                #交费类型
                "payType":["年交"],
                # 满期保险金
                "totalMoney":[""],
                # 身故保险金
                "deathMoney":[""],
                #航空意外
                "airplane_death":["10000000"],
                #公共交通工具意外
                "common_traffic":["1000000"],
                #驾车意外包赔金额
                "private_death":["1000000"],
                #一般意外身故
                "terrorist_death":["500000"],
                #意外医疗保险金
                "accident_medical":["25000"],
                # 意外住院津贴
                "accident_hospital":["250/天"]

            },
            "planB": {
                # 年龄段
                "year": [str(i) for i in range(51, 56)],
                # 性别
                "gender": ["M", "F"],
                # 保障期限
                "days": ["25y"],
                # 交费类型
                "payType": ["年交"],
                # 满期保险金
                "totalMoney": [""],
                # 身故保险金
                "deathMoney": [""],
                # 航空意外
                "airplane_death": ["10000000"],
                # 公共交通工具意外
                "common_traffic": ["1000000"],
                # 驾车意外包赔金额
                "private_death": ["1000000"],
                # 一般意外身故
                "terrorist_death": ["500000"],
                # 意外医疗保险金
                "accident_medical": ["25000"],
                # 意外住院津贴
                "accident_hospital": ["250/天"]

            }
        }
        for k in plan:
            for v in product(plan[k]["year"],plan[k]["gender"],plan[k]["days"],plan[k]["payType"],plan[k]["totalMoney"],plan[k]["deathMoney"],plan[k]["airplane_death"],plan[k]["common_traffic"],plan[k]["private_death"],plan[k]["terrorist_death"],plan[k]["accident_medical"],plan[k]["accident_hospital"]):
                if v[2] == "25y":
                    interval = "10"
                else:
                    interval = "20"
                data = Helper.formatProductInfo( plan[k],v )
                data["interval"] = interval

                yield data
    #计算保费
    def calcPremium(self):
        params = '''
        age: 36
        sex: 女
        baoxianqijian: 35
        jiaofeifangshi: 12
        jiaofeiqijian: 20
        '''
        data = Helper.perfectJson( params )
        for v in self.genProductInfo():
            print( v )
            data["age"] = v["year"]
            data["sex"] = "女" if v["gender"] == "F" else "男"
            data["baoxianqijian"] = v["days"].replace("y","")
            data["jiaofeiqijian"] = v["interval"]
            payload = urlencode( data ).replace("+","")
            r = requests.post( url = domain[self.name]["url"],headers=domain[self.name]["header"],data=payload)
            if r.status_code == 200 and r.json()["success"] == "true":
                price = Helper.decimalParse( r.json()["value"]["rt"] )
                result = {
                    "name": self.name,
                    "company": "华夏保险",
                    "price": price
                }
                v["totalMoney"]  = Helper.decimalParse(float( price ) * 1.38)
                v["deathMoney"] = Helper.decimalParse( float( price) * 1.60)
                v.update( result )
                Helper.mergeTable( self.name,self.mysql,v )
